import http from "@/axios/http";

// 定义基础URL
const BASE_URL = 'http://192.140.166.207:8088/order/'

// 统一的请求处理函数
const handleRequest = async (url, params) => {
    try {
        const res = await http.post(url, params);
        if (res.code === 200) {
            return res.data;
        }
        throw new Error(res.message || '请求失败');
    } catch (error) {
        console.error(`API请求错误: ${error.message}`, error);
        throw error;
    }
};

const order = {
    // 查询订单列表
    // selectAll: (params) => handleRequest(`${BASE_URL}/QueryOrdersByPagination`, params),
    // 查询单个订单
    selectById: (params) => handleRequest(`${BASE_URL}/selectById`, params),
    // 更新订单
    update: (params) => handleRequest(`${BASE_URL}/update`, params),
    // 删除订单
    // delete: (params) => handleRequest(`${BASE_URL}/delById`, params),

    selectAll: (params: any) => {
        return http.post(BASE_URL+"QueryOrdersByPagination", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },

    delete: (params: any) => {
        return http.delete(BASE_URL+"delById", params).then(res=>{
            if(res.code == 200){
                return res.data;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'DELET failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during DELET:', error);
            throw error;
        });
    },

    /*
    * 平均客单价（已完成订单）
    * */
    averageOrderPrice: (params: any) => {
        return http.get(BASE_URL+"averageOrderPrice", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },

      /*
    * 总销售额（已完成订单）
    * */
    totalSales: (params: any) => {
        return http.get(BASE_URL+"totalSales", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },

      /*
    * 总订单量（含待付款 / 已完成）
    * */
      totalOrders: (params: any) => {
        return http.get(BASE_URL+"totalOrders", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },

     /*
    * 课程销量排名（按订单数
    * */
     courseSalesRanking: (params: any) => {
        return http.get(BASE_URL+"courseSalesRanking", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },

    /*
    * 课程销售额排名（按收入）
    * */
    courseSalesRankingByIncome: (params: any) => {
        return http.get(BASE_URL+"courseSalesRankingByIncome", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                throw new Error(res.message || 'FIND failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during FIND:', error);
            throw error; // 继续抛出错误，让调用方可以捕获
        });
    },


};

export default order;